package com.huike.web.controller.system;

import com.huike.clues.service.MpDictDataService;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.domain.entity.SysDictData;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.entity.MPSysDictData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description TODO
 * @Author guan
 * @Date 2023-08-29 8:35
 */
@RestController
@RequestMapping("/system/dict/data")
@Api(tags = "数据字典数据信息接口")
@Slf4j
public class MpDictDataController extends BaseController {

    @Resource
    private MpDictDataService mpDictDataService;


    @ApiOperation("字典列表")
    @GetMapping("/list")
    public TableDataInfo list(MPSysDictData mpSysDictData) {
        log.info("字典列表sysDictData:{}", mpSysDictData);
        //开启分页
        startPage();
        List list = mpDictDataService.getList(mpSysDictData);
        TableDataInfo dataTable = getDataTable(list);
        return dataTable;
    }

    @ApiOperation("根据字典类型查询字典数据信息")
    @GetMapping("/type/{dictType}")
    public AjaxResult listByDictType(@PathVariable String dictType) {
        log.info("根据字典类型查询字典数据信息:{}",dictType);

        List list = mpDictDataService.listByDictType(dictType);

        return AjaxResult.success(list);
    }

    @ApiOperation("查询字典数据详细")
    @GetMapping("/{dictCode}")
    public AjaxResult listByDictCode(@PathVariable Long dictCode) {
        log.info("查询字典数据详细dictCode:{}", dictCode);
        MPSysDictData mpSysDictData = mpDictDataService.listByDictCode(dictCode);
        return AjaxResult.success(mpSysDictData);
    }

    @ApiOperation("新增字典数据")
    @PostMapping
    public AjaxResult add(@RequestBody MPSysDictData mpSysDictData) {
        log.info("新增字典数据sysDictData:{}", mpSysDictData);
        mpDictDataService.add(mpSysDictData);
        log.info("新增字典数据影响的行数：{}",mpSysDictData);
        return AjaxResult.success();
    }


    /**
     * 修改保存字典数据
     * @param mpSysDictData
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @ApiOperation("修改保存字典数据")
    @PutMapping
    public AjaxResult updateDict(@RequestBody MPSysDictData mpSysDictData){
        log.info("修改开始了");
        mpDictDataService.updateDict(mpSysDictData);
        return AjaxResult.success();
    }


    /**
     * 删除字典数据
     * @param dictCodes
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @ApiOperation("删除字典数据")
    @DeleteMapping("/{dictCodes}")
    public AjaxResult deleteDict(@PathVariable List<Long> dictCodes){
        log.info("开始批量删除数据");
        mpDictDataService.deleteDict(dictCodes);
        return AjaxResult.success();
    }
}
